layui.use(['laypage', 'layer'], function(){
    var laypage = layui.laypage
        ,layer = layui.layer;

    //总页数大于页码总数
    laypage.render({
        elem: 'demo1'
        ,count: getProductSum() //数据总数
        // ,pages: getProductSum()/8 //页码
        ,limit:8
        ,jump: function(obj){
            // obj.limit = 8;
            // obj.pages = (obj.count)/8;
            let pageDatas ={"count":obj.count,"curr":obj.curr,"pages":obj.pages,"limit":obj.limit};
            console.log("分页参数",obj)
            $.ajax({
                url: "http://localhost:8080/back/product/findAll",
                dataType: "json",
                data: {"page":pageDatas.curr,"limit":pageDatas.limit},
                success: function (res) {
                    let val = '';
                    console.log("前端产品返回的数据",res)
                    for (let i = 0; i < res.data.length; i++) {

                        val +='<div>\n' +
                            '                                <div>\n' +
                            '                                    <img src="'+res.data[i].imgHref+'" alt="" onclick="detail('+res.data[i].id+')">\n' +
                            '                                </div>\n' +
                            '                                <div>\n' +
                            '                                    <div>'+res.data[i].name+'</div>\n' +
                            '                                    <div>'+res.data[i].forWho+'</div>\n' +
                            '                                    <div>\n' +
                            '                                        <div>价格</div>\n' +
                            '                                        <div>'+res.data[i].normalPrice+'</div>\n' +
                            '                                    </div>\n' +
                            '                                </div>\n' +
                            '                            </div>'
                    }
                    $("#bookContent").html(val)

                },
            })
        }
    })
})

//打开图书详情
function detail(id) {
    sessionStorage.setItem("bookPid",id);
    window.location.href="/front_page/front/html/display-detail.html"

}

//取得产品总条数
function getProductSum() {
    let count;

    $.ajax({
        url:'http://localhost:8080/back/front/productSum',
        dataType:'json',
        async:false,//关闭异步
        success:function (data) {
            count = data.data

        }
    })
    return count;
}

